package com.danciyixue.boot.client.userCurrentProgram.controller;

import com.danciyixue.boot.common.result.Result;
import com.danciyixue.boot.core.security.util.SecurityUtils;
import com.danciyixue.boot.dcyx.model.form.UserCurrentProgramForm;
import com.danciyixue.boot.dcyx.model.vo.CurrentProgramVO;
import com.danciyixue.boot.dcyx.model.vo.UserCurrentProgramVO;
import com.danciyixue.boot.dcyx.service.UserCurrentProgramService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 用户当前教材项控制器
 */
@Tag(name = "08. 选中教材", description = "选择教材")
@RestController
@RequestMapping("/api/client/v1/user-current-program")
@RequiredArgsConstructor
public class DcyxUserCurrentProgramController {

    private final UserCurrentProgramService userCurrentProgramService;

    /**
     * 获取用户当前选中教材项
     */
    @GetMapping("/current")
    @Operation(summary = "获取用户当前选中教材项")
    public Result<UserCurrentProgramVO> getCurrentProgram() {
        // 获取当前登录用户ID
        Long userId = SecurityUtils.getUserId();
        UserCurrentProgramVO vo = userCurrentProgramService.getCurrentProgramByUserId(userId);
        return Result.success(vo);
    }

    /**
     * 更新用户当前选中教材项
     */
    @PostMapping("/current")
    @Operation(summary = "更新用户当前选中教材项")
    public Result<Void> updateCurrentProgram(@RequestBody UserCurrentProgramForm form) {
        // 获取当前登录用户ID并设置到表单
        Long userId = SecurityUtils.getUserId();
        form.setUserId(userId);
        boolean result = userCurrentProgramService.updateCurrentProgram(form);
        return Result.judge(result);
    }

    /**
     * 获取当前用户选中的教材详细信息
     */
    @GetMapping("/current-detail")
    @Operation(summary = "获取当前用户选中的教材详细信息")
    public Result<CurrentProgramVO> getCurrentProgramDetail() {
        CurrentProgramVO result = userCurrentProgramService.getCurrentProgramDetailByUserToken();
        return Result.success(result);
    }
}